package cn.wen.forum.common.util;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

/**
 * @ClassName RSAUtils
 * @Author By wen
 * @Description
 * @Date 2023/8/18 14:44
 * @Version 1.0
 */
public class RSAUtils {

    private static final String CHARSET_NAME = "utf-8";
    private static final String TYPE = "RSA";

    private static final String PUB_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsyqZiIArbD6dXsrHHvfu\n" +
            "p7wYHoLvQfLzo15z5cuDMW5WtyHa3xUVTmFOw4BV63AyZnsE68RXKtzDKb96U0W3\n" +
            "TtZv322mHCzYIWZO75oet0lqmml03VvzLCLvTZABXn9v+/Wz9iKgK5RrdJHlCxbC\n" +
            "GTTxbfTh02t5oqwVwYwahksuU18rsWsdqQcXSJCrZBsdPIu3+LEt9qgimiJ+x3uL\n" +
            "1cl/Z1sSzm2C6ePsDcf++LcGPLWo77TmXUEMLujC1dHjynSIVQzH6NE42q9pNVUl\n" +
            "aKoO+v9AKbstyhgTYndYhkeyJzp4DTONqGGRhYylY0n9aMgBdoYOyHVXMb3KMRes\n" +
            "0QIDAQAB";
    private static final String PRI_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCzKpmIgCtsPp1e\n" +
            "ysce9+6nvBgegu9B8vOjXnPly4Mxbla3IdrfFRVOYU7DgFXrcDJmewTrxFcq3MMp\n" +
            "v3pTRbdO1m/fbaYcLNghZk7vmh63SWqaaXTdW/MsIu9NkAFef2/79bP2IqArlGt0\n" +
            "keULFsIZNPFt9OHTa3mirBXBjBqGSy5TXyuxax2pBxdIkKtkGx08i7f4sS32qCKa\n" +
            "In7He4vVyX9nWxLObYLp4+wNx/74twY8tajvtOZdQQwu6MLV0ePKdIhVDMfo0Tja\n" +
            "r2k1VSVoqg76/0Apuy3KGBNid1iGR7InOngNM42oYZGFjKVjSf1oyAF2hg7IdVcx\n" +
            "vcoxF6zRAgMBAAECggEAYIGww7LuMNclY1rsmMTJu3X4s+6pO6fHDpeg7AWMPEMH\n" +
            "2IsW3xT3LgoCKBJntXLw4vyesPRIU6EoSsOF+3bQ/1lIV1wznneVT6BRIoSsyON1\n" +
            "1Yi6V0XhHUQQDIJmCmWwh1JYxlruVasTmm2/pxJyKKu45BaE0aHRmgsBiQiTfQP4\n" +
            "wXQpOJgqrE1U7cGZSQ1cnlmXmbkw3dc644tm+mYqd4EX4g4ayYTy9xtTHUVuuxZG\n" +
            "/GqHjSWUBfHQ+npoD/p4X5Tn7/zju4bp1mmIvHccHHGfuVxIRWZkByJFblVDC68G\n" +
            "rRhvVsRcR4wX9IddPUmaSyWo+Ib9Httlxi660I7gAQKBgQDlf/qM/qLZTWAt5dwq\n" +
            "Ul67jfW8A/8BaXBGHoLMmnxDMM6PejlUEbWw+sA0yhGZeTarICYE1zaZAmhAHRLH\n" +
            "CGo459HZZQlEIuBfsumrWFPu4QE4btTK53HinzClk5QncJePglLpaciNt3VEeSPM\n" +
            "SDWciQOidWtIW7H2XQeuQYJO0QKBgQDH2sL4QQwxnZuw9woOBSYq7pyOB2oqWqSC\n" +
            "8BZn9+PuNUoTGNbXVMc4ONiOLZU55yriXozBRcJHyfKKwbp3RU/DPKMUFLH4DCYy\n" +
            "r2jdgOCiGdLpmTKPpMqkm+TdawYFHAaTWkRXo2mzivVXKQ9AlgAzCVmUGCE1yVSH\n" +
            "MHk2+8L+AQKBgQCovjfAEb41XrIYLUl7iMMEznVfP+Tw6dDWjbqIrBIv6SQH1Mtw\n" +
            "J/Fa5pQKfIUCPmDW5DVwG7mWOXHjqjzTQTmxC6p9WM++XJJzav5qsPBN1PzrvHoT\n" +
            "kqdnBLtlEyS89wlJSH6kBtaidMDxjsymeHWK2idk/Ubblig8x9xM4Pfp8QKBgQC2\n" +
            "5Kz+UVDeavSa+0i5Ud7+9QRSOdjfT7ozlZF7rfBqA1x5o4ag4pW6DtOnMXHZ3oJx\n" +
            "TMOU78j+t4H9l5KUKdr3uZsn+lRvwnaGdd4KrzFjRyyr2CXcLJvCDmRgeNu2e1JN\n" +
            "MRNP/Fz0ENu62vl5Sox+KDVRANALj+aKHUMgnT9yAQKBgEX6ckfIahnwUO8BHP4p\n" +
            "XC+C/LNaOyP+Oo42oLnFyyHs/KH5Rr68XClFqD1ZAxVN45qmllR0AwNgw/KmWU+w\n" +
            "Xuat6wVk6uoeHebKYYWhOPKJUqjnEJ5Fd4TqGGvXiKzwgHZWtktYYko42wOs26Yx\n" +
            "a3xcndYr2WYHNxec5BF0aN64";

    /**
     * RSA公钥加密
     *
     * @param str       加密字符串
     * @param publicKey 公钥
     * @return 密文
     * @throws Exception 加密过程中的异常信息
     */
    public static String encrypt(String str, String publicKey) throws Exception {
        //base64编码的公钥
        byte[] decoded = Base64.decodeBase64(publicKey);
        RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance(TYPE).generatePublic(new X509EncodedKeySpec(decoded));
        //RSA加密
        Cipher cipher = Cipher.getInstance(TYPE);
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        String outStr = Base64.encodeBase64String(cipher.doFinal(str.getBytes(CHARSET_NAME)));
        return outStr;
    }

    /**
     * RSA私钥解密
     *
     * @param str        加密字符串
     * @param privateKey 私钥
     * @return 铭文
     * @throws Exception 解密过程中的异常信息
     */
    public static String decrypt(String str, String privateKey) throws Exception {
        //64位解码加密后的字符串
        byte[] inputByte = Base64.decodeBase64(str.getBytes(CHARSET_NAME));
        //base64编码的私钥
        byte[] decoded = Base64.decodeBase64(privateKey);
        RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance(TYPE).generatePrivate(new PKCS8EncodedKeySpec(decoded));
        //RSA解密
        Cipher cipher = Cipher.getInstance(TYPE);
        cipher.init(Cipher.DECRYPT_MODE, priKey);
        String outStr = new String(cipher.doFinal(inputByte));
        return outStr;
    }

    public static void main(String[] args) throws Exception {
        String publicKey = PUB_KEY;
        String privateKey = PRI_KEY;
        String text = "ck";
        String encodeText = encrypt(text, publicKey);
        System.out.println("加密后：" + encodeText);
        System.out.println("解密后：" + decrypt(encodeText, privateKey));
    }

}
